<template>
  <BasicTable @register="registerTable">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              label: '个人信息',
              icon: 'clarity:info-standard-line',
              onClick: handleProfileDesc.bind(null, record),
            },
            {
              label: '查看小组',
              icon: 'clarity:info-standard-line',
              onClick: handleGroupDesc.bind(null, record),
            },
          ]"
        />
      </template>
    </template>
  </BasicTable>
  <ProfileDesc @register="registerModal" />
</template>
<script lang="ts" setup>
  import { defaultActionColumn, defaultTableSettings } from "/@/hooks/core/useDefault";
  import { BasicTable, TableAction, useTable } from "/@/components/Table";
  import { useRouter } from "vue-router";
  import ProfileDesc from "/@/views/default/account/ProfileDesc.vue";
  import { getTutorList, getUserInfo } from "/@/api/core/user";
  import { useModal } from "/@/components/Modal";
  import { teacherBasicColumns } from "/@/views/default/data";
  import { searchFormConfig } from "/@/views/student/group/join/data";
  import { useUserStore } from "/@/store/modules/user";

  const userStore = useUserStore();

  const [registerTable] = useTable({
    title: "教师列表",
    rowKey: "userId",
    api: getTutorList,
    columns: teacherBasicColumns,
    useSearchForm: true,
    formConfig: searchFormConfig,
    beforeFetch(value) {
      if (!value.collegeId) {
        value.collegeId = userStore.getUserInfo.college.collegeId;
      }
    },
    actionColumn: {
      width: 240,
      ...defaultActionColumn,
    },
    ...defaultTableSettings,
  });

  const [registerModal, { openModal }] = useModal();

  async function handleProfileDesc(record) {
    openModal(true, await getUserInfo(record.userId, record.userCode));
  }

  const router = useRouter();

  function handleGroupDesc(record) {
    router.push({
      name: "Detail",
      query: { userId: record.userId },
    });
  }
</script>
